package com.shujia.flinkcore;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.Arrays;

public class wordCount {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setBufferTimeout(0);
        env.setParallelism(2);
        env.enableCheckpointing(5000);
        DataStreamSource<String> src = env.socketTextStream("hadoop102", 8888);
        SingleOutputStreamOperator<Tuple2<String, Integer>> wordDS = src
                .flatMap((words, out) -> {
                    String[] split = words.split(",");
                    for (String word : split) {
                        out.collect(word);
                    }
                }, Types.STRING)
                .map(word -> Tuple2.of(word, 1), Types.TUPLE(Types.STRING, Types.INT));
        SingleOutputStreamOperator<Tuple2<String, Integer>> resDS = wordDS
                .keyBy(value -> value.f0)
                .sum(1);
        resDS.print();
        env.execute();
    }
}
